<?php

namespace App\Http\Controllers\web;

use App\Http\Controllers\Controller;
use App\Models\users\UsersAmountRecordModel;
use App\Service\users\UsersService;
use App\Service\users\WithdrawService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use lxs\api\ApiResponse;
use lxs\tools\Tools;

class FinanceController extends Controller
{
    /**
     * 提取公共数据
     */
    public function extracted(Request $request): array
    {
        $uid = $request->web_uid;
        $post = $request->input();

        return array($uid, $post);
    }


    /**
     * 获取财务提现页面数据
     */
    public function getWithdrawData(Request $request): JsonResponse
    {

        list($uid, $post) = $this->extracted($request);

        $userInfo = UsersService::instance()->getUserDetail($uid);
        $userInfo->withdraw_rules = Tools::Config('WEB.withdraw_rules');

        // 提现列表数据
        $searchData = [];
        if (isset($post['data_time'])) {
            $searchData['data_time'] = $post['data_time'];
        }
        $withdrawData = WithdrawService::instance()->getWithdrawData($uid, $post['page'], $post['page_size'], $searchData);

        return ApiResponse::apiResponseData(200, '获取财务体现页面数据成功', [
            'user_info' => $userInfo,
            'lists' => $withdrawData,
        ]);
    }


    /**
     * 财务提现表单提交
     */
    public function userWithdrawSubmit(Request $request): JsonResponse
    {

        list($uid, $post) = $this->extracted($request);

        $result = WithdrawService::instance()->userWithdrawSubmit($uid, $post);

        if ($result['code'] == 200) {
            return ApiResponse::success('提现申请已提交，等待人工审核', ['dateTime' => date('Y-m-d H:i:s', time())]);
        }
        return ApiResponse::error($result['message'], $result['code']);
    }


    /**
     * 获取用户收益明细页面数据
     */
    public function getUserIncomeData(Request $request): JsonResponse
    {

        list($uid, $post) = $this->extracted($request);

        $page = $post['page'];
        $pageSize = $post['page_size'];
        $dateTime = $post['data_time'];
        $type = $post['type'];

        $userInfo = UsersService::instance()->getUserBalance($uid);
        $userInfo->amount_difference = Tools::Config('SINGLE.amount_difference');

        list ($lists, $count) = UsersService::instance()->getUserIncomeRecordData($uid, $page, $pageSize, $dateTime, $type);
        foreach ($lists as $key => $item) {
            $lists[$key]['create_at'] = date('Y-m-d H:i:s', strtotime($item['create_at']));
        }

        return ApiResponse::apiResponseData(200, '获取用户收益明细页面数据成功', [
            'user_info' => $userInfo,
            'lists' => $lists,
            'total' => $count,
        ]);
    }


}
